import api from '../../../utils/api';
import pipe from '../../../assets/js/pipe.js';
var loginName = localStorage.getItem("_loginName");
export default {
    data(){
        return{
            resData:[],
            props: {
                label: 'name',
                children: 'zones'
            },
            pages:1,//分页
            isDisabled:true,//分页按钮是否禁用
            isDisabled1:false,//
            dialogFormVisible:false,//任务清单 实施对象弹窗
            dialogFormVisible1:false,//任务清单 审核状态弹窗
            formLabelWidth:'60px',//
            //新增信息
            taskData:{
                taskName:'',//任务名称
                taskType:[],//任务类型 
                marineBoardCode:[],//海事局
                radarStationCode:[],//雷达站
                startDate:'',//计划开始时间
                endDate:'',//计划结束时间
                taskObj:"",//实施对象
                personName:"",//海事局审核人
                content:'',//任务内容
            },
            query1:'',//v-model 任务类型
            query2:'',//v-model 海事局
            query3:'',//v-model 雷达站
            selectHsj:"",//选择海事局
            seletRadarCode:'',//选择雷达站
            selectCode:'',//设备树
            deviceData:[],//选择实施对象
            examineData:[],//审核人
            selectName:"",//保存选择设备树节点内容
            roleId:[],//角色id  string
            loginName:[],//登陆名 string 
            userName:[],//用户名 string
            loginNames:"",
            userNames:"",
            roleIds:'',
            radarName:"",//雷达name值
        }
    },
    mounted(){
        this.findTaskType();//任务类型
        this.addPlandata();//选择海事局
        this.selectRadar();//选择雷达站
    },
    methods:{
        //返回上一页
        backPage(){
            this.$parent.$router.go(-1);
        },
        //任务类型
        findTaskType:async function(){
            let data = {
                params:"cateCode=TASK_TYPE"
            }
            const res = await this.$axios._post(api.findTaskMsgList,data);
            if(res.data.success){
                this.taskData.taskType = res.data.data;
            }
        },
        //海事局数据
        addPlandata:async function(){
            let data={
                params:"orgTypeCode=HAI_SHI"
            }
            const res = await this.$axios._post(api.findOrg ,data);
            if(res.data.success){ 
                this.taskData.marineBoardCode = res.data.data;
            }
        },
        //选中option
        selectItem(e){
            this.query3='';//清空雷达站
            if(e == '湛江海事局'){
                e = 'ZHAN_JIANG_HAISHIJU';
            }else if(e == '广州海事局'){
                e = 'GUANG_ZHOU_HAISHIJU';
            }else if(e == '汕头海事局'){
                e = 'SHAN_TOU_HAISHIJU';
            }else if(e == '茂名海事局'){
                e = 'MAO_MING_HAISHIJU';
            }else if(e == '惠州海事局'){
                e = 'HUI_ZHOU_HAISHIJU';
            }else if(e == '珠海海事局'){
                e = 'ZHU_HAI_HAISHIJU';
            }else{
                e = 'DONG_GUAN_HAISHIJU';
            }
            this.selectHsj = e;
            this.selectRadar(e);
        },
        //雷达站
        selectRadar:async function(code){
            let data = {
                params:"orgCode="+code
            }
            const res = await this.$axios._post(api.findLeida ,data);
            if(res.data.success){
                this.taskData.radarStationCode = res.data.data;
            }
        },
        selectRadarItem(e){
            this.seletRadarCode = e;
        },
        //判断是否已选择海事局，若不，弹出提示
        judgeCondition(){
            if(!this.selectHsj){
                this.$message({
                    message:"请先选择海事局",
                    type:"warning"
                })
            }
        },

        //实施对象
        getDevice:async function(){
            this.deviceData = [];
            if(this.seletRadarCode){
                this.dialogFormVisible = true;
                let data = {
                    params:"ldzCode="+this.seletRadarCode//雷达站编码
                }
                const res = await this.$axios._post(api.getDevice,data);
                if(res.data.success){
                    this.forData(res.data.data);
                }
            }else{
                this.$message({
                    message:"请先选择雷达站",
                    type:"warning"
                })
            }
           
        },
        //循环设备树数据
        forData(res){
            let obj = res[0];
            obj.list = [];
            this.deviceData.push(obj);
            res.splice(0,1);
            orderData(this.deviceData);
            function orderData(newRes){
                for(let i=0;i<newRes.length;i++){
                    for(let j=0;j<res.length;j++){
                        if(newRes[i].nodeCode == res[j].parentCode){
                            var obj1 = res[j];
                            obj1.list = [];
                            newRes[i].list.push(obj1);
                            res.splice(j,1);
                            j--;
                        }
                    }
                    orderData(newRes[i].list);
                }
            }
        },
        //选中实施对象
        selectWhich(code,name){
            this.selectCode = code;
            this.selectName = name;
        },
        //保存选择设备树节点内容
        saveMenuData(){
            this.dialogFormVisible = false;
            //界面内容
            this.taskData.taskObj = this.selectName;
        },
        //审核人
        findHsjUser:async function(){
            this.dialogFormVisible1 = true;
            let data = {
                params:"page="+this.pages+"&rows="+7
            }
            const res = await this.$axios._post(api.findHsjUser,data);
            if(res.data.success){
                this.examineData = res.data.data;
            }
        },
        prevPage(){
            this.pages--;
            if(this.pages<=1){
                this.isDisabled=true;
            }
            this.findHsjUser(this.pages);
        },
        nextPage(){
            this.pages++;
            if(this.pages>1){
                this.isDisabled=false;
            }
            this.findHsjUser(this.pages);
        },
        //checkbox勾选
        selectData(selection, row){
           this.loginName.push(row.loginName);
           this.userName.push(row.userName);
           this.roleId.push(row.roleId);
           this.loginNames = this.loginName.join(",");
           this.userNames = this.userName.join(",");
           this.roleIds = this.roleId.join(",");
        },
        //审核人选择后，点击保存
        saveExamineData(){
            this.dialogFormVisible1 = false;
            this.taskData.personName = this.userNames;
            this.loginName = [];
            this.userName = [];
            this.roleId = [];
        },


         //点击保存新增计划
        savePlan:async function(){
            window.console.log(loginName);
            //开始时间 结束时间 均为object  ====>字符串
            var startDate=pipe.formatData(this.taskData.startDate);
            var endDate=pipe.formatData(this.taskData.endDate);
            if(this.taskData.taskName&&this.query1&&this.selectHsj&&this.seletRadarCode&&this.taskData.content&&this.selectCode&&this.userNames&&this.taskData.startDate&&this.taskData.endDate){
                let data = {
                    //任务名称  任务类型 海事局 雷达站 任务内容 实施对象 执行人 开始时间 结束时间 用户登录名 角色id
                    params:"taskName="+this.taskData.taskName+"&taskType="+this.query1+"&marineBoardCode="+this.selectHsj+"&radarStationCode="+
                    this.seletRadarCode+"&taskContent="+this.taskData.content+"&implementDest="+this.selectCode+"&executorId="+
                    this.loginNames+"&startDate="+startDate+"&endDate="+endDate+"&loginName="+loginName+"&roleId="+this.roleIds
                }
                const res = await this.$axios._post(api.addRenQing,data);
                if(res.data.success){
                    if(res.data.code){
                        this.$router.push("/workProgress");
                    }
                }
            }else{
                this.$message({
                    message:"请填写完整再提交",
                    type:"warning"
                });
                return;
            }
           
        }
    }
}